class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return null;
}
if (root.val < key) {
root.right = deleteNode(root.right, key);
return root;
}
if (root.val > key) {
root.left = deleteNode(root.left, key);
return root;
}
if (root.left == null && root.right == null) {
root = null;
return root;
}
if (root.left != null &&root.right == null) {
root = root.left;
return root;
}
if (root.left == null && root.right != null) {
root = root.right;
return right;
}
if (root.left != null && root.right != null) {
int val = findMaxInLeftTree(root.left);
root.val = val;
root.left = deleteNode(root.left, val);
return root;
}
return root;
}
int findMaxInLeftTree(TreeNode node) {
if (node == null) {
return 0;
}
if (node.right == null) {
return node.val;
}
if (node.right == null && node.left == null) {
return node.val;
}
return findMaxInLeftTree(node.right);
}
}